package com.sservice.service.utils.wx_third_platform;

import com.sservice.common.Constants;
import com.sservice.service.common.AdvancedUtil;
import com.sservice.service.service.redis.Redis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by suweiming on 2018/8/20.
 */
@Component
public class UrlUtils {
    @Autowired
    AdvancedUtil advancedUtil;
    @Autowired
    RequestUtil requestUtil;
    /**
     * 代公众号发起网页授权 step1
     *
     * @param redirectUrl
     * @param appId
     * @return
     */
    public static String getUserLoginUrl(String redirectUrl, String appId) {
        return String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect",
                appId, redirectUrl, "snsapi_userinfo", "xxxx_state", Constants.componentAppId
        );
    }

    public  String getAccessTokenByCode(String code, String appId) throws IOException {
        String componentAccessToken = requestUtil.getComponentAccessTokenStr();
        return String.format("https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s",
                appId, code, Constants.componentAppId,componentAccessToken

        );
    }



    public  String getRefreshAccessToken(String refreshToken, String appId) throws IOException {
        String componentAccessToken = "";
        if(StringUtils.isBlank(Redis.get(Constants.KEY_THIRD_PLATFORM_ACCESS_TOKEN))){
            requestUtil.requestToken();
        }
        componentAccessToken = Redis.get(Constants.KEY_THIRD_PLATFORM_ACCESS_TOKEN);
        return String.format("https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid=%s&grant_type=refresh_token&component_appid=%s&component_access_token=%s&refresh_token=%s",
                appId, Constants.componentAppId,componentAccessToken,refreshToken

        );
    }

    /**
     * 用户授权
     *
     * @return
     */
    public  String thirdPlatFormAuthUrl(String url) {
        String preAuthCode = "";
        try {
            requestUtil.requestPreAuthCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        preAuthCode = Redis.get(Constants.KEY_PRE_AUTH_CODE);
        return "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" + Constants.componentAppId + "&pre_auth_code=" +
                preAuthCode + "&redirect_uri=" + (advancedUtil.urlEncodeUTF8(url));
    }

    /**
     * 第三方平台获取token url
     *
     * @return
     */
    public static String thirdPlatFormGetToken() {
        return "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
    }

    public static String thirdPlatFormGetPreAuthCode(String token) {
        return "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + token;
    }

    public static String getTicketByAuthCode(String token) {
        return "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + token;
    }

    public static String getRefreshTicketBy(String token) {
        return "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + token;

    }


    public static String getAutherInfo(String token) {
        return "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" + token;
    }

}
